﻿#include "stdafx.h"
#include "skinstatic.h"
#include "StaticSkin.h"

CSkinStatic::CSkinStatic(void)
{
	//m_pStaticSkin = GetSkin().GetStaticSkin();
	m_uTextFormat = 0;
}

CSkinStatic::~CSkinStatic(void)
{
	HookWindow((HWND)NULL);
}
BOOL CSkinStatic::LoadSkin()
{
    CAutoEnableSkin a(m_bEnableSkin);
	m_pStaticSkin = GetSkin().GetStaticSkin();
    if (!m_pStaticSkin)
    {
        return FALSE;
    }
	SetWindowPos(m_hWnd,NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER); 
    return TRUE;
}
BOOL CSkinStatic::InstallSkin(HWND hWnd)
{
    if (!hWnd || !LoadSkin())
    {
        return FALSE;
    }
	HookWindow( (HWND)NULL);

	int r = HookWindow( hWnd );

	if(m_pStaticSkin == NULL)
		m_pStaticSkin = GetSkin().GetStaticSkin();

	if(m_pStaticSkin == NULL)
		m_bEnableSkin = FALSE;
	else 
		m_bEnableSkin = TRUE;

#ifdef _WIN64
	LONG_PTR style = GetWindowLongPtr(m_hWnd, GWL_STYLE);
	LONG_PTR exstyle = GetWindowLongPtr(m_hWnd, GWL_EXSTYLE);
#else
	DWORD style = GetWindowLong(m_hWnd, GWL_STYLE);
	DWORD exstyle = GetWindowLong(m_hWnd, GWL_EXSTYLE);
#endif

	
	if((style & SS_RIGHT) == SS_RIGHT)
		m_uTextFormat |= DT_RIGHT;
	else if((style & SS_CENTER) == SS_CENTER)
		m_uTextFormat |= DT_CENTER;
	else
		m_uTextFormat |= DT_LEFT;
//	m_uTextFormat |= DT_END_ELLIPSIS;

	switch (style & SS_TYPEMASK)
	{
	case SS_LEFT:
		m_uTextFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK;
		break;

	case SS_CENTER:
		m_uTextFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK;
		break;

	case SS_RIGHT:
		m_uTextFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK;
		break;

	case SS_SIMPLE:
		m_uTextFormat = DT_LEFT | DT_SINGLELINE;
		break;

	case SS_LEFTNOWORDWRAP:
		m_uTextFormat = DT_LEFT | DT_EXPANDTABS;
		break;

	default:
        {
            HookWindow( (HWND)NULL);
		    return FALSE;
        }
	}

	if (style & SS_NOPREFIX)
		m_uTextFormat |= DT_NOPREFIX;

	if ((style & SS_TYPEMASK) != SS_SIMPLE)
	{
		if (style & SS_CENTERIMAGE)
			m_uTextFormat |= DT_SINGLELINE | DT_VCENTER;
#ifndef SS_EDITCONTROL
#define SS_EDITCONTROL      0x00002000L
#endif
		if (style & SS_EDITCONTROL)
			m_uTextFormat |= DT_EDITCONTROL;
		if (style & SS_ENDELLIPSIS)
			m_uTextFormat |= DT_SINGLELINE | DT_END_ELLIPSIS;
		if (style & SS_PATHELLIPSIS)
			m_uTextFormat |= DT_SINGLELINE | DT_PATH_ELLIPSIS;
		if (style & SS_WORDELLIPSIS)
			m_uTextFormat |= DT_SINGLELINE | DT_WORD_ELLIPSIS;
	}

	SetWindowPos(m_hWnd,NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER); 

    return TRUE;
}
void CSkinStatic::OnPaint()
{
	CPaintDC dc(CWnd::FromHandle(m_hWnd));
	DrawStatic(&dc);
}
void CSkinStatic::OnSetText(WPARAM wp,LPARAM lp)
{
	Default();
    CWnd* pWnd = CWnd::FromHandle(m_hWnd);
    CAutoReleaseDC pDC(pWnd);
	pDC = pWnd->GetDC();
	DrawStatic(pDC);
}
void CSkinStatic::OnNcPaint(HRGN rgn1)
{
    CWnd* pWnd = CWnd::FromHandle(m_hWnd);
    CAutoReleaseDC pDC(pWnd);
    pDC = pWnd->GetDC();
	DrawStatic(pDC);
}
BOOL CSkinStatic::OnEraseBkgnd(CDC *pDC)
{
	CRect rtWindow;
	CStatic *pStatic = (CStatic*)CWnd::FromHandle(m_hWnd);
	pStatic->GetWindowRect(rtWindow);
	rtWindow.OffsetRect(-rtWindow.left,-rtWindow.top);
	if(m_pStaticSkin->m_bTransparent)
	{
		CBrush brushBack(m_pStaticSkin->m_colorBack);
		pDC->FillRect(rtWindow,&brushBack);
	}
	else
	{
		m_pStaticSkin->DrawImageSection(pDC,rtWindow,m_pStaticSkin->m_imageBack);
	}
	return TRUE;
}
void CSkinStatic::DrawStatic(CDC *pDC)
{
	
	if(m_pStaticSkin == NULL)
		return;
	CRect rtWindow;
	CStatic *pStatic = (CStatic*)CWnd::FromHandle(m_hWnd);
	pStatic->GetWindowRect(rtWindow);
	rtWindow.OffsetRect(-rtWindow.left,-rtWindow.top);

	int nOldMode = pDC->SetBkMode(TRANSPARENT);
	if(m_pStaticSkin->m_bTransparent)
	{
		CBrush brushBack(m_pStaticSkin->m_colorBack);
		pDC->FillRect(rtWindow,&brushBack);
	}
	else
	{
		m_pStaticSkin->DrawImageSection(pDC,rtWindow,m_pStaticSkin->m_imageBack);
	}
	CString strCaption;
	pStatic->GetWindowText(strCaption);
	CFont *pFont = NULL;
	CFont *pOld = NULL;
	if(m_pStaticSkin->m_bAutoFont)
	{
		pFont = pStatic->GetFont(); 
		pOld = (CFont*)pDC->SelectObject( pFont );
	}
	else
	{
		pFont = new CFont;
		pFont->CreateFontIndirect(&(m_pStaticSkin->m_fontStatic));
		pOld = (CFont*)pDC->SelectObject( pFont );
	}
	pDC->DrawText(strCaption,rtWindow,m_uTextFormat);

	HICON hIcon = pStatic->GetIcon();
	ICONINFO piconinfo;
	CRect rtIcon;
	if(hIcon)
	{
		GetIconInfo(hIcon,&piconinfo); //取图标信息，需要释放piconinfo里的bitmap资源
		rtIcon = rtWindow;

	#ifdef _WIN64
		LONG_PTR style = GetWindowLongPtr(m_hWnd, GWL_STYLE);
	#else
		DWORD style = GetWindowLong(m_hWnd, GWL_STYLE);
	#endif

		if (style & SS_CENTERIMAGE)
		{
			rtIcon.left = (rtWindow.right - rtWindow.left) / 2 - piconinfo.xHotspot;
			rtIcon.top = (rtWindow.bottom - rtWindow.top) / 2 - piconinfo.yHotspot;
			rtIcon.right = rtIcon.left + piconinfo.xHotspot*2;
			rtIcon.bottom = rtIcon.top + piconinfo.yHotspot*2;
		}

			
		
		DrawIconEx(pDC->GetSafeHdc(), rtIcon.left,rtIcon.top, hIcon,rtIcon.Width(),rtIcon.Height(),0, NULL, DI_NORMAL);
        piconinfo.hbmColor ? DeleteObject(piconinfo.hbmColor) : NULL;
        piconinfo.hbmMask ? DeleteObject(piconinfo.hbmMask) : NULL;
	}

	pDC->SetBkMode(nOldMode);
	pDC->SelectObject(pOld);
	if(!m_pStaticSkin->m_bAutoFont)
		delete pFont;
}